package v3.chapter2.synchronizedOneThreadIn;

public class Run {

	public static void main(String[] args) {
		ObjectService service=new ObjectService();
		ThreadA a =new ThreadA(service);
		a.setName("A");
		a.start();
		
		ThreadB b=new ThreadB(service);
		b.setName("B");
		b.start();

	}

}

class ThreadA extends Thread{
	private ObjectService service;
	public ThreadA(ObjectService service) {
		super();
		this.service=service;
	}
	
	@Override
	public void run() {
		super.run();
		service.serviceMethod();
	}
}

class ThreadB extends Thread{
	private ObjectService service;
	public ThreadB(ObjectService service) {
		super();
		this.service=service;
	}
	
	@Override
	public void run() {
		super.run();
		service.serviceMethod();
	}
}

class ObjectService{
	public void serviceMethod() {
		try {
			synchronized (this) {
				System.out.println("begin time="+System.currentTimeMillis());
				Thread.sleep(2000);
				System.out.println("end time="+System.currentTimeMillis());
			}
		}catch(InterruptedException e) {
		e.printStackTrace();	
		}
	}
}